package com.example.todoapp.http.controllers;

import com.example.todoapp.data.entities.TagItem;
import com.example.todoapp.data.entities.TodoItem;
import com.example.todoapp.http.ResponseStruct;
import com.example.todoapp.http.TodoItemRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Optional;
@RestController
public class TagItemController {
    @Autowired
    TagItemController todoItemRepository;
    @GetMapping("/tags")
    public ResponseStruct list1(@RequestParam MultiValueMap<String, Object> params) {
        // pagination
        int page = (int) Optional.ofNullable(params.getFirst("page")).orElse(1);
        int size = (int) Optional.ofNullable(params.getFirst("size")).orElse(10);
        PageRequest pageRequest = PageRequest.of(page, size);
        // filter
        Specification<TagItem> specification = (root, query, criteriaBuilder) -> {
            return null;
        };
        if(params.containsKey("title")){

        }

        Iterable<TagItem> list = todoItemRepository.findAll();

        return ResponseStruct.wrap(list);
    }
    @GetMapping("/tags/{id}")
    public ResponseStruct show1(@PathVariable long id) {
        TagItem todoItem = todoItemRepository.findFirstById(id);

        if (todoItem == null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", id);
            return new ResponseStruct(-1, "todo-item not found", data);
        }

        return ResponseStruct.wrap(todoItem);
    }
    @PostMapping("/tags")
    public ResponseStruct store1(@RequestBody TodoItemRequest request) {

        TagItem todoItem = new TodoItem();
        BeanUtils.copyProperties(request, todoItem);
        todoItem.setCreatedAt(LocalDateTime.now());

        todoItemRepository.save(todoItem);

        return ResponseStruct.wrap(todoItem);
    }
    @PostMapping("/tags/{id}")
    public ResponseStruct update1(@RequestBody TodoItemRequest request, @PathVariable long id) {
        TagItem todoItem = todoItemRepository.findFirstById(id);

        if (todoItem == null) {
            HashMap<String, Object> data = new HashMap<>();
            data.put("id", id);
            return new ResponseStruct(-1, "todo-item not found", data);
        }

        if (request.getTitle() != null) {
            todoItem.setTitle(request.getTitle());
        }
        if (request.getDescription() != null) {
            todoItem.setDescription(request.getDescription());
        }
        if (request.getDate() != null) {
            todoItem.setDate(request.getDate());
        }
        todoItem.setUpdatedAt(LocalDateTime.now());

        todoItemRepository.save(todoItem);

        return ResponseStruct.wrap(todoItem);
    }
}
